﻿using Microsoft.Practices.Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZookeeperViewer.Component;
using ZookeeperViewer.Model;

namespace ZookeeperViewer.ViewModel
{
    public class ConnectSettingWindowVM : NotifyObject
    {
        private string _connectionString = "192.168.8.100:2181";
        public string ConnectionString
        {
            get { return _connectionString; }
            set
            {
                _connectionString = value;
                OK.RaiseCanExecuteChanged();
                this.RaisePropertyChanged("ConnectionString");
            }
        }

        private string _timeout = "15000";
        public string Timeout
        {
            get { return _timeout; }
            set
            {
                _timeout = value;
                OK.RaiseCanExecuteChanged();
                this.RaisePropertyChanged("Timeout");
            }
        }

        public DelegateCommand OK { get; set; }

        public ConnectSettingWindowVM()
        {
            OK = new DelegateCommand(DoOK, DoCanOK);
        }

        private void DoOK() { }

        private bool DoCanOK()
        {
            string[] hostandport = ConnectionString.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int timeout = 0;
            int port = 0;
            if (hostandport.Length == 2 && int.TryParse(Timeout, out timeout) && timeout > 0 && int.TryParse(hostandport[1], out port) && port > 0)
            {
                return true;
            }
            else return false;
        }
    }
}
